import dip
from MA.watcher import ModuleWatcher
from celery import Celery
import scipy.misc
import scipy.ndimage
from logging import info
import coloredlogs, logging

logger = logging.getLogger('your-module')
coloredlogs.install(level='DEBUG')
coloredlogs.install(level='DEBUG', logger=logger)
from logging import debug

app = Celery('tasks', broker='redis://localhost:6379/0', backend='redis://localhost')

@app.task
def test(image_name, output_name_prefix):
    img = dip.imread(image_name)
    img = scipy.ndimage.zoom(img,1500/img.shape[0])
    info(img.shape)
    result = dip.get_exclude(img)
    for name,image in result.items():
        scipy.misc.imsave(output_name_prefix + name + '.png', image)
    return ""


@app.task
def handle_hospital_data(image_name):
    img = dip.imread(image_name)
    imginfo = dip.readInfo(img)
    imgpath = '/home/d/data/yunnan/'+ imginfo['id']+'.png'
    logger.warning(imgpath)
    try:
        scipy.misc.imsave(imgpath, img)
    except:
        pass


@app.task
def convert_img_format(fname:str):
    debug('converting : ' + fname)
    if fname.endswith('.jpg') or fname.endswith('.JPG'):
        return
    index = fname.rfind('.')
    if index<0:
        raise Exception('cannot find ext')
    img = scipy.misc.imread(fname)
    fname = fname[:index] + '.jpg'
    scipy.misc.imsave(fname,img)
    return